$(function(){
    var obj = {
        userTel: '',
        cities: ['越城','诸暨','新昌','上虞','嵊州','柯桥'],
        searchCity: '',
        searchKey: '',
        ajaxStatus: false,
        pageNo: 0,
        pageSize: 20,
        init: function () {
            var that = this;
            if(!$.authIsLogin(false,true)) return;
            that.userTel = localStorage.getItem('myTel') || '';
            that.addEvent();
            $.getPositionCity(function (cityName) {
                that.cityName = cityName;
                $('#currentCity').html(that.cityName);
                that.fillSearchList();
            });
        },
        addEvent: function () {
            var that = this;
            // 选择城市
            $('#currentCity').click(function (event) {
                event.stopPropagation();
                if($('.jobsBox').hasClass('blur')){
                    $('.jobsBox').removeClass('blur');
                    $('.searchList').slideUp();
                }
                else{
                    $('.jobsBox').addClass('blur');
                    $('.searchList').slideDown();
                    $('body').click(function () {
                        $(this).unbind();
                        $('.jobsBox').removeClass('blur');
                        $('.searchList').slideUp();
                    })
                }
            });
            //搜索
            $('#search').click(function () {
                that.getFamouseCompanyList(0);
            });
            // 加载更多
            $('#loadingMore').click(function () {
                that.getFamouseCompanyList();
            });
        },
        getFamouseCompanyList: function(pageNo){
            var that = this;
            if(that.ajaxStatus) return;
            that.ajaxStatus = true;
            var search = $('#searchIpt').val();
            if(!isNaN(pageNo)) that.pageNo = pageNo;
            that.pageNo++;
            $.getData({
                url: $.getAjaxHost('def') + 'cxjy/zpinfo/getFamouseCompanyList',
                type: 'post',
                contentType: 'application/json',
                data: {
                    key:encodeURI(search),
                    qx: encodeURI(that.searchCity ==='全市'?'':that.searchCity),
                    pageNo: that.pageNo,
                    pageSize: that.pageSize
                },
                success: function (res) {
                    that.ajaxStatus = false;
                    if(res.obj.length>0){
                        $('.logListBox').removeClass('none');
                        $('.noData').addClass('none');
                        that.fillLogList(res.obj,res.total);
                    }else {
                        $('.logListBox').addClass('none');
                        $('.noData').removeClass('none');
                    }
                },
                error: function (res) {
                    that.ajaxStatus = false;
                }
            })
        },
        fillSearchList: function () {
            var that = this;
            var data = {};
            data.localCity = that.cityName;
            data.list = that.cities;
            var html = template('drop', data);
            $('#searchList').html(html);
            if(data.localCity){
                that.searchCity = data.localCity;
                $('.citySite.location').addClass('active');
            }else{
                $('.citySite.total').addClass('active');
            }
            that.getFamouseCompanyList(0);
            // 选择区域
            $('.citySite').click(function () {
                that.searchCity = $(this).attr('data-name');
                $('.citySite').removeClass('active');
                $(this).addClass('active');
                $('#currentCity').html(that.searchCity);
                that.getFamouseCompanyList(0);
            });
        },
        fillLogList: function (list,total) {
            var that = this;
            var data = {};
            data.list = list;
            var html = template('list', data);
            if(that.pageNo == 1) $('#logList').html(html);
            else $('#logList').append(html);
            if($('#logList .applyMsg').length< Number(total)){
                $('#loadingMore').removeClass('none');
            }else{
                $('#loadingMore').addClass('none');
            }
            $('.meeting').unbind();
            // 进入名企
            $('.meeting').click(function () {
                var id = $(this).attr('data-id');
                var title = $(this).attr('data-name');
                location.href = './jobSearchList.html?type=1&columnId='+ id+ '&title='+ title;
            })
        }
    };
    obj.init();
});